using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FiltronASP.Models;
using FiltronASP.Models.DAO;

namespace FiltronASP.Helpers.CatalogueMenu
{
    public class CatalogueMenuHelper : Controller
    {
        public static string generateMenuWIX(int languageId, int pageId) {
            string htmlToReturn = String.Empty;
            int menuCounter = 1;
            int galleryCounter = 1;
            string currentLink = String.Empty;
            string languageName = Helpers.Translations.translationHelper.getLanguageName(languageId);
            Models.DAO.GalleryImageDAO GalleryImageDAO = new Models.DAO.GalleryImageDAO();
            FiltronASP.Models.DAO.MenuItemDAO MenuItemDAO = new FiltronASP.Models.DAO.MenuItemDAO();
            IQueryable<FiltronASP.Models.MenuItem> MenuItemCollection = MenuItemDAO.FindByLanguageIdAndMenuNameAndItemPublished(languageId, "WIX", true);
            if (MenuItemCollection != null)
            {
                foreach (FiltronASP.Models.MenuItem menuElement in MenuItemCollection)
                {
                    galleryCounter = 1;
                    IQueryable<GalleryImage> imagesCollection = GalleryImageDAO.FindByGalleryNameAndPage("Icons", 125);
                    if (imagesCollection != null)
                    {
                        foreach (GalleryImage image in imagesCollection)
                        {
                            if (menuCounter == galleryCounter)
                            {
                                if (image != null)
                                {
                                    currentLink = image.gallery_image_link;
                                    galleryCounter++;
                                }
                            }
                            else
                            {
                                galleryCounter++;
                            }
                        }
                    }
                    if (menuElement.page_id == pageId)
                    {
                        htmlToReturn += String.Format("<li class=\"active {3}\"><div class=\"imageAnchor\"><a href=\"{0}\"><img src=\"{1}\" alt=\"\" /></a></div><div class=\"textAnchorBlock\"><a href=\"{0}\"><span>{2}</span></a></div></li>", menuElement.item_link + "#contentHolder", currentLink, menuElement.item_name, languageName);
                    }
                    else
                    {
                        htmlToReturn += String.Format("<li class=\"{3}\"><div class=\"imageAnchor\"><a href=\"{0}\"><img src=\"{1}\" alt=\"\" /></a></div><div class=\"textAnchorBlock\"><a href=\"{0}\"><span>{2}</span></a></div></li>", menuElement.item_link + "#contentHolder", currentLink, menuElement.item_name, languageName);
                    }
                    menuCounter++;
                }
            }
            return htmlToReturn;
        }

        public static string generateMenuFiltron(int languageId, int pageId)
        {
            string htmlToReturn = String.Empty;
            int menuCounter = 1;
            int galleryCounter = 1;
            string currentLink = String.Empty;
            string languageName = Helpers.Translations.translationHelper.getLanguageName(languageId);
            Models.DAO.GalleryImageDAO GalleryImageDAO = new Models.DAO.GalleryImageDAO();
            FiltronASP.Models.DAO.MenuItemDAO MenuItemDAO = new FiltronASP.Models.DAO.MenuItemDAO();
            IQueryable<FiltronASP.Models.MenuItem> MenuItemCollection = MenuItemDAO.FindByLanguageIdAndMenuNameAndItemPublished(languageId, "Filtron", true);
            if (MenuItemCollection != null)
            {
                foreach (FiltronASP.Models.MenuItem menuElement in MenuItemCollection)
                {
                    galleryCounter = 1;
                    IQueryable<GalleryImage> imagesCollection = GalleryImageDAO.FindByGalleryNameAndPage("Icons", 124);
                    if (imagesCollection != null)
                    {
                        foreach (GalleryImage image in imagesCollection)
                        {
                            if (menuCounter == galleryCounter)
                            {
                                if (image != null)
                                {
                                    currentLink = image.gallery_image_link;
                                    galleryCounter++;
                                }
                            }
                            else
                            {
                                galleryCounter++;
                            }
                        }
                    }
                    if (menuElement.page_id == pageId)
                    {
                        htmlToReturn += String.Format("<li class=\"active {3}\"><div class=\"imageAnchor\"><a href=\"{0}\"><img src=\"{1}\" alt=\"\" /></a></div><div class=\"textAnchorBlock\"><a href=\"{0}\"><span>{2}</span></a></div></li>", menuElement.item_link + "#contentHolder", currentLink, menuElement.item_name, languageName);
                    }
                    else
                    {
                        htmlToReturn += String.Format("<li class=\"{3}\"><div class=\"imageAnchor\"><a href=\"{0}\"><img src=\"{1}\" alt=\"\" /></a></div><div class=\"textAnchorBlock\"><a href=\"{0}\"><span>{2}</span></a></div></li>", menuElement.item_link + "#contentHolder", currentLink, menuElement.item_name, languageName);
                    }
                    menuCounter++;
                }
            }
            return htmlToReturn;
        }

        public static string GenerateLastModifiedWix(int? languageId)
        {
            if (languageId == null)
            {
                return String.Empty;
            }   //if
            TemplateDAO templateDAO = new TemplateDAO();
            Template template = templateDAO.FindByLangAndName(languageId.Value, "Settings");
            if (template == null)
            {
                return String.Empty;
            }   //if
            VariableDAO variableDAO = new VariableDAO();
            Variable variable = variableDAO.FindByTemplateAndLanguageAndName(template.template_id, languageId.Value, "last_modified_wix");
            if (variable == null)
            {
                return String.Empty;
            }   //if
            VariableValueDAO variableValueDAO = new VariableValueDAO();
            IQueryable<VariableValue> variableValues = variableValueDAO.FindByVariableId(variable.variable_id);
            if (variableValues == null)
            {
                return String.Empty;
            }   //if
            if (variableValues.Count() == 0)
            {
                return String.Empty;
            }   //if
            String result = String.Empty;
            foreach (VariableValue value in variableValues)
            {
                result = value.variable_value;
                break;
            }   //for
            if (!String.IsNullOrEmpty(result))
            {
                result = result.Replace("<br />", " ");
                result = result.Replace("<br/>", " ");
                result = result.Replace("<br>", " ");
                result = result.Replace("<p>", "<p style='width:50%; text-align: right; float: right'>");
            }   //if
            return result;
        }

        public static string GenerateLastModifiedFiltron(int? languageId)
        {
            if (languageId == null)
            {
                return String.Empty;
            }   //if
            TemplateDAO templateDAO = new TemplateDAO();
            Template template = templateDAO.FindByLangAndName(languageId.Value, "Settings");
            if (template == null)
            {
                return String.Empty;
            }   //if
            VariableDAO variableDAO = new VariableDAO();
            Variable variable = variableDAO.FindByTemplateAndLanguageAndName(template.template_id, languageId.Value, "last_modified_filtron");
            if (variable == null)
            {
                return String.Empty;
            }   //if
            VariableValueDAO variableValueDAO = new VariableValueDAO();
            IQueryable<VariableValue> variableValues = variableValueDAO.FindByVariableId(variable.variable_id);
            if (variableValues == null)
            {
                return String.Empty;
            }   //if
            if (variableValues.Count() == 0)
            {
                return String.Empty;
            }   //if
            String result = String.Empty;
            foreach (VariableValue value in variableValues)
            {
                result = value.variable_value;
                break;
            }   //for
            if (!String.IsNullOrEmpty(result))
            {
                result = result.Replace("<br />", " ");
                result = result.Replace("<br/>", " ");
                result = result.Replace("<br>", " ");
                result = result.Replace("<p>", "<p style='width:50%; text-align: right; float: right'>");
            }   //if
            return result;
        }

    }
}
